package com.example.photography.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.photography.bean.UserBean;
import com.example.photography.db.DBHelper;

public class UserInfoDao {
    private DBHelper mHelper;

    public UserInfoDao(DBHelper mHelper) {
        this.mHelper = mHelper;
    }

    //储存数据
    public void saveUserInfo(UserBean.UserInfo userInfo){
        if (userInfo==null){
            return;
        }
        //1. 获取数据库链接
        SQLiteDatabase db = mHelper.getReadableDatabase();
        //2. 执行保存语句
        ContentValues values = new ContentValues();
        values.put(UserInfoTable.id_unique,userInfo.getId_unique());
        values.put(UserInfoTable.head_url,userInfo.getHead_url());
        values.put(UserInfoTable.hr_sign,userInfo.getHr_sign());
        values.put(UserInfoTable.name,userInfo.getName());

        db.replace(UserInfoTable.TAB_NAME,null,values);
    }
    //查询数据
    public UserBean.UserInfo getUserInfo(String id_unique){
        //1. 获取数据库链接
        SQLiteDatabase db = mHelper.getReadableDatabase();
        //2.
        String sql = "select * from "+UserInfoTable.TAB_NAME+" where "+UserInfoTable.id_unique + " =?";
        Cursor cursor = db.rawQuery(sql,new String[]{id_unique});
        UserBean.UserInfo userInfo = null;
        if (cursor.moveToNext()){
            userInfo = new UserBean.UserInfo();

            //封装数据
            userInfo.setId_unique(cursor.getString(cursor.getColumnIndex(UserInfoTable.id_unique)));
            userInfo.setHead_url(cursor.getString(cursor.getColumnIndex(UserInfoTable.head_url)));
            userInfo.setHr_sign(cursor.getString(cursor.getColumnIndex(UserInfoTable.hr_sign)));
            userInfo.setName(cursor.getString(cursor.getColumnIndex(UserInfoTable.name)));
        }
        //关闭资源
        cursor.close();
        //返回数据
        return userInfo;
    }
    //修改hr_sign数据
    public void update_hrsign(String id_unique,String hr_sign){
        if (hr_sign == null){
            return;
        }
        //获取数据库链接
        SQLiteDatabase db = mHelper.getReadableDatabase();

        //执行更新操作
        db.execSQL("update "+UserInfoTable.TAB_NAME +" set hr_sign=? where id_unique=?",new Object[]{hr_sign,id_unique});
        db.close();
    }

}
